package com.tdr.myandroid.db;

import android.database.Cursor;

import com.tdr.myandroid.model.ActionObject;
import com.tdr.myandroid.model.ConditionObject;
import com.tdr.myandroid.model.EventObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2014/7/16.
 */
public class ConditionDao  {
    private ArrayList<ConditionObject> conditionsList = new ArrayList<ConditionObject>();

    public ArrayList<ConditionObject> getConditionsList() {
        return conditionsList;
    }

    public void updateAllConditions() {
        conditionsList.clear();

        Cursor cur = DatabaseHelper.getDbHelper().getReadableDatabase().rawQuery("select * from conditions", null);

        while (cur.moveToNext()) {
            int id = cur.getInt(cur.getColumnIndex("event_id"));
            int typeInt = cur.getInt(cur.getColumnIndex("trigger_type"));
            String conditionString = cur.getString(cur.getColumnIndex("trigger_condition"));

            ConditionObject newCondition = EventObject.createCondition(typeInt, id);
            newCondition.setCondition(conditionString);
            conditionsList.add(newCondition);
        }
    }

    public ArrayList<ConditionObject> getActionsById(int id ) {
        ArrayList<ConditionObject> returnConditionList = new ArrayList<ConditionObject>();

        for (ConditionObject singleCondition : conditionsList) {
            if (singleCondition.getId() == id) {
                returnConditionList.add(singleCondition);
            }
        }
        return returnConditionList;
    }
}
